document.addEventListener("DOMContentLoaded", function () {
    const searchEngines = [
        // 第行
        { name: "百度翻译(中英)", url: "https://fanyi.baidu.com/#zh/en/" },
        { name: "百度翻译(英中)", url: "https://fanyi.baidu.com/#en/zh/" },
        { name: "谷歌翻译", url: "https://translate.google.com/?text=" },
        { name: "", url: "" },
        // 第行
        { name: "ScienceDirect", url: "https://www.sciencedirect.com/search?qs=" },
        { name: "IEEE", url: "https://ieeexplore.ieee.org/search/searchresult.jsp?queryText=" },
        { name: "Springer Link", url: "https://link.springer.com/search?query=" },
        { name: "mdpi", url: "https://www.mdpi.com/search?q=" },
        // 第行
        { name: "AIAA", url: "https://arc.aiaa.org/action/doSearch?AllField=" },
        { name: "aminer", url: "https://www.aminer.cn/search/pub?q=" },
        { name: "ResearchGate", url: "https://www.researchgate.net/search.Search.html?query=" },
        { name: "doi.org", url: "https://doi.org/" },
        // 第行
        { name: "百度学术", url: "https://xueshu.baidu.com/s?wd=" },
        { name: "知网", url: "https://kns.cnki.net/kns8s/defaultresult/index?kw=" },
        { name: "万方", url: "https://s.wanfangdata.com.cn/paper?q=" },
        { name: "谷粉学术", url: "https://so1.typicalgame.com/scholar?q=" },
        // 第行
        { name: "Stack Exchange", url: "https://stackexchange.com/search?q=" },
        { name: "GitHub", url: "https://github.com/search?q=" },
        { name: "Stack Overflow", url: "https://stackoverflow.com/search?q=" },
        { name: "CSDN", url: "https://so.csdn.net/so/search?q=" },
        // 第行
        { name: "知乎", url: "https://www.zhihu.com/search?q=" },
        { name: "必应知乎", url: "https://cn.bing.com/search?q=site:zhihu.com+" },
        { name: "哔哩哔哩", url: "https://search.bilibili.com/all?keyword=" },
        { name: "必应B站", url: "https://cn.bing.com/search?q=site:bilibili.com+" },
        // 第行
        { name: "Google", url: "https://www.google.com/search?q=" },
        { name: "Google Scholar", url: "https://scholar.google.com/scholar?q=" },
        { name: "Wikipedia", url: "https://en.wikipedia.org/w/index.php?search=" },
        { name: "", url: "" },
        // 第行
        { name: "百度", url: "https://www.baidu.com/s?wd=" },
        { name: "百度图片", url: "https://image.baidu.com/search/index?tn=baiduimage&fm=result&ie=utf-8&word=" },
        { name: "百度贴吧", url: "https://tieba.baidu.com/f/search/res?ie=utf-8&qw=" },
        // { name: "百度贴吧", url: "https://tieba.baidu.com/f?kw=" },
        { name: "", url: "" },
        // 第行
        { name: "菜鸟教程", url: "https://www.runoob.com/?s=" },
        { name: "易百教程", url: "https://www.yiibai.com/search?kw=" },
        { name: "", url: "" },
        { name: "", url: "" },
    ];

    const buttonContainer = document.getElementById("buttonContainer");
    const buttons = [];
    searchEngines.forEach(function (engine) {
        const button = document.createElement("button");
        button.textContent = engine.name;
        button.addEventListener("click", function () {
            if (engine.name.trim() === "") {
                return; // 按钮上没有文字时禁用按钮
            }
            const searchQuery = encodeURIComponent(document.getElementById("searchInput").value);
            const searchUrl = engine.url + searchQuery;
            window.open(searchUrl, "_blank");
        });
        buttonContainer.appendChild(button);
        buttons.push(button);
    });

    let focusedIndex = 0;
    function handleDirectionKeys(event) {
        if (event.key === "ArrowRight") {
            focusedIndex = (focusedIndex + 1) % buttons.length;
            buttons[focusedIndex].focus();
        } else if (event.key === "ArrowLeft") {
            focusedIndex = (focusedIndex - 1 + buttons.length) % buttons.length;
            buttons[focusedIndex].focus();
        } else if (event.key === "ArrowUp") {
            focusedIndex = (focusedIndex - 4) % buttons.length;
            buttons[focusedIndex].focus();
        } else if (event.key === "ArrowDown") {
            focusedIndex = (focusedIndex + 4) % buttons.length;
            buttons[focusedIndex].focus();
        }
    }

    document.addEventListener("keydown", handleDirectionKeys);
    // 输入框获得焦点时，移除方向键监听
    document.getElementById("searchInput").addEventListener("focus", function () {
        document.removeEventListener("keydown", handleDirectionKeys);
    });
    // 输入框失去焦点时，重新添加方向键监听
    document.getElementById("searchInput").addEventListener("blur", function () {
        document.addEventListener("keydown", handleDirectionKeys);
    });
    document.getElementById("searchInput").focus(); // 让编辑框获得焦点
});
